package com.djhu.springsimple.beans.context;

import com.djhu.springsimple.beans.core.BeanDefinition;
import com.djhu.springsimple.beans.factory.AutowireBeanFactory;
import com.djhu.springsimple.beans.factory.BaseBeanFactory;
import com.djhu.springsimple.beans.io.ResourceLoader;
import com.djhu.springsimple.beans.xml.XmlBeanDefinitionReader;

import java.util.Map;

/**
 * @author by zw on 2018/4/16.
 */
public class ClassPathXmlApplicationContext extends BaseApplicationContext{


    private String configLocation;

    public ClassPathXmlApplicationContext(String configLocation) throws Exception {
        this(configLocation, new AutowireBeanFactory());
    }

    public ClassPathXmlApplicationContext(String configLocation, BaseBeanFactory beanFactory) throws Exception {
        super(beanFactory);
        this.configLocation = configLocation;
        refresh();
    }


    @Override
    protected void loadBeanDefinitions(BaseBeanFactory beanFactory) throws Exception {
        XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(new ResourceLoader());
        xmlBeanDefinitionReader.load(configLocation);
        for (Map.Entry<String, BeanDefinition> entry:xmlBeanDefinitionReader.getRegistry().entrySet()){
            beanFactory.registerBeanName(entry.getKey(),entry.getValue());
        }
    }
}